using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace TD.TheGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PlayerComponent : DrawableGameComponent
    {
        SpriteBatch _batch;
        //public Sprite bullet;

        public int Points
        {
            get;
            set;
        }

        private int currentArrow = 0;
        public ArrowSprite[] arrows = new ArrowSprite[15];
        public Sprite archer;
        KeyBoardInput keyInput;
        public PlayerComponent(Game game)
            : base(game)
        {
            Points = 0;
            // TODO: Construct any child components here
            keyInput = (KeyBoardInput)Game.Services.GetService(typeof(KeyBoardInput));

            _batch = new SpriteBatch(Game.GraphicsDevice);
            // TODO: Add your initialization code here
            //init bullets
            for (int i = 0; i < arrows.Length; i++)
            {
                ArrowSprite arrowSample = new ArrowSprite(Game, new Vector2(0,0), false, 100, SpriteEnumState.Past);
                arrows[i] = arrowSample;
            }
            //init player
            archer = new Archer(game, new Vector2(0, 500), true, 100, SpriteEnumState.Normal);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            archer.Update(gameTime);

            foreach (var arrow in arrows)
            {
                arrow.Update(gameTime);
            }

            if (archer.State == SpriteEnumState.Shoot && archer.endSprite)
            {
                InitArrow();
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// Initialize the Arrow
        /// </summary>
        private void InitArrow()
        {
            if (currentArrow >= arrows.Count())
            {
                currentArrow = 0;
            }
            if (arrows[currentArrow].Active == false)
            {
                arrows[currentArrow].position.X = archer.BoundingRectangle.X + 30;
                arrows[currentArrow].position.Y = archer.BoundingRectangle.Y + 20;
                arrows[currentArrow].Active = true;
                currentArrow++;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _batch.Begin();
            archer.Draw(gameTime);
            //bullet.Draw(gameTime);
            foreach (var arrow in arrows)
            {
                arrow.Draw(gameTime);
            }
            _batch.End();
            base.Draw(gameTime);
        }

        //update State of arrows (past, normal or future)
        public void updateArrows(SpriteEnumState state, int damage)
        {
            foreach (var arrow in arrows)
            {
                arrow.Upgrade(state, damage);
            }
        }

        //update the reloadTime of the archer, if maxLooptime is lower, reloadtime is faster
        public void updateArcher(int maxLoopTime)
        {
            archer.maxLoopTime = maxLoopTime;
        }

        //reset arrow and archer
        public void Reset()
        {
            foreach (var arrow in arrows)
            {
                arrow.Reset();
            }
            archer.Reset();
        }

        /// <summary>
        /// Update the time it takes for the archer to reload.
        /// </summary>
        public int ReloadTime
        {
            get
            {
                return archer.maxLoopTime;
            }
            set
            {
                archer.maxLoopTime = value;
            }
        }
    }
}
